home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / python2.6 / dist-packages / computerjanitorapp / ui_cli_tests.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  13.9 KB  |  299 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import unittest
  5. import computerjanitor
  6. import computerjanitorapp
  7.  
  8. class MockCruft(object):
  9.     
  10.     def __init__(self, name):
  11.         self._name = name
  12.         self.cleaned = False
  13.         self.post_cleaned = False
  14.  
  15.     
  16.     def get_name(self):
  17.         return self._name
  18.  
  19.     
  20.     def cleanup(self):
  21.         self.cleaned = True
  22.  
  23.  
  24.  
  25. class MockPlugin(object):
  26.     
  27.     def __init__(self, crufts):
  28.         self._crufts = crufts
  29.         self.post_cleaned = False
  30.  
  31.     
  32.     def get_cruft(self):
  33.         for cruft in self._crufts:
  34.             yield cruft
  35.         
  36.  
  37.     
  38.     def post_cleanup(self):
  39.         self.post_cleaned = True
  40.  
  41.  
  42.  
  43. class MockPluginManager(object):
  44.     
  45.     def __init__(self, plugins):
  46.         self._plugins = plugins
  47.  
  48.     
  49.     def get_plugins(self):
  50.         return self._plugins
  51.  
  52.  
  53.  
  54. class MockState(object):
  55.     
  56.     def __init__(self, enabled):
  57.         self._enabled = set(enabled)
  58.  
  59.     
  60.     def is_enabled(self, name):
  61.         return name in self._enabled
  62.  
  63.     
  64.     def enable(self, name):
  65.         if name not in self._enabled:
  66.             self._enabled.add(name)
  67.         
  68.  
  69.     
  70.     def disable(self, name):
  71.         if name in self._enabled:
  72.             self._enabled.remove(name)
  73.         
  74.  
  75.     
  76.     def load(self, filename):
  77.         self.load_filename = filename
  78.  
  79.     
  80.     def save(self, filename):
  81.         self.save_filename = filename
  82.  
  83.  
  84.  
  85. class MockOptionParser(object):
  86.     
  87.     def __init__(self):
  88.         self.help_printed = False
  89.  
  90.     
  91.     def print_help(self):
  92.         self.help_printed = True
  93.  
  94.  
  95.  
  96. class MockApplication(object):
  97.     
  98.     def __init__(self, enabled):
  99.         self.state = MockState(enabled)
  100.         self.parser = MockOptionParser()
  101.  
  102.     
  103.     def verify_apt_cache(self):
  104.         pass
  105.  
  106.     
  107.     def remove_whitelisted(self, crufts):
  108.         return crufts
  109.  
  110.  
  111.  
  112. class MockOptions(object):
  113.     
  114.     def __init__(self):
  115.         self.all = None
  116.         self.state_file = 'foo'
  117.         self.no_act = None
  118.         self.verbose = None
  119.  
  120.  
  121.  
  122. class CommandLineUserInterfaceTests(unittest.TestCase):
  123.     
  124.     def setUp(self):
  125.         self.app = MockApplication([
  126.             'foo'])
  127.         self.cruft_names = [
  128.             'foo',
  129.             'bar']
  130.         self.crufts = [ MockCruft(name) for name in self.cruft_names ]
  131.         self.cruftdict = dict((lambda .0: for c in .0:
  132. (c.get_name(), c))(self.crufts))
  133.         self.plugin = MockPlugin(self.crufts)
  134.         self.pm = MockPluginManager([
  135.             self.plugin])
  136.         self.ui = computerjanitorapp.CommandLineUserInterface(self.app, self.pm, mustberoot = False)
  137.         self.options = MockOptions()
  138.  
  139.     
  140.     def testInsistsOnBeingRoot(self):
  141.         self.ui.mustberoot = True
  142.         self.assertRaises(computerjanitor.Exception, self.ui.run, None, None)
  143.  
  144.     
  145.     def testFindsTheRightCruft(self):
  146.         self.assertEqual(self.ui.find_cruft(), self.crufts)
  147.  
  148.     
  149.     def testShowsTheRightCruftTheRightWay(self):
  150.         
  151.         def mock_find_cruft():
  152.             return self.crufts
  153.  
  154.         
  155.         def mock_show_one_cruft(name, desc, state, width):
  156.             output.append((name, state))
  157.  
  158.         output = []
  159.         self.ui.find_cruft = mock_find_cruft
  160.         self.ui.show_one_cruft = mock_show_one_cruft
  161.         self.ui.show_cruft(MockOptions(), None)
  162.         self.assertEqual(output, sorted([
  163.             ('foo', 'removable'),
  164.             ('bar', 'ignored')]))
  165.  
  166.     
  167.     def testIgnoresCruftCorrectly(self):
  168.         self.ui.ignore(self.options, [
  169.             'foo'])
  170.         self.assertFalse(self.app.state.is_enabled('foo'))
  171.  
  172.     
  173.     def testUnignoresCruftCorrectly(self):
  174.         self.ui.unignore(self.options, [
  175.             'bar'])
  176.         self.assert_(self.app.state.is_enabled('bar'))
  177.  
  178.     
  179.     def testCleansUpEnabledCruftWithDashDashAll(self):
  180.         self.options.all = True
  181.         self.ui.cleanup(self.options, [])
  182.         self.assert_(self.cruftdict['foo'].cleaned)
  183.  
  184.     
  185.     def testDoesNotCleanUpEnabledCruftWithDashDashAllWhenNoActIsSet(self):
  186.         self.options.all = True
  187.         self.options.no_act = True
  188.         self.ui.cleanup(self.options, [])
  189.         self.assertFalse(self.cruftdict['foo'].cleaned)
  190.  
  191.     
  192.     def testCleansUpRequestedEnabledCruft(self):
  193.         self.ui.cleanup(self.options, [
  194.             'foo'])
  195.         self.assert_(self.cruftdict['foo'].cleaned)
  196.  
  197.     
  198.     def testDoesNotCleanUpDisaabledCruftWithDashDashAll(self):
  199.         self.options.all = True
  200.         self.ui.cleanup(self.options, [])
  201.         self.assertFalse(self.cruftdict['bar'].cleaned)
  202.  
  203.     
  204.     def testCleansUpRequestedDisabledCruft(self):
  205.         self.ui.cleanup(self.options, [
  206.             'bar'])
  207.         self.assert_(self.cruftdict['bar'].cleaned)
  208.  
  209.     
  210.     def testRunsPostCleanup(self):
  211.         self.ui.cleanup(self.options, [])
  212.         self.assert_(self.plugin.post_cleaned)
  213.  
  214.     
  215.     def testDoesNotRunPostCleanupWhenNoActIsSet(self):
  216.         self.options.no_act = True
  217.         self.ui.cleanup(self.options, [])
  218.         self.assertFalse(self.plugin.post_cleaned)
  219.  
  220.     
  221.     def testRaisesExceptionForUnknownCruft(self):
  222.         self.assertRaises(computerjanitor.Exception, self.ui.cleanup, self.options, [
  223.             'unknown'])
  224.  
  225.     
  226.     def testHelpCallsParserPrintHelp(self):
  227.         self.ui.help(None, None)
  228.         self.assert_(self.app.parser.help_printed)
  229.  
  230.     
  231.     def setup_run(self):
  232.         names = [
  233.             'show_cruft',
  234.             'cleanup',
  235.             'ignore',
  236.             'unignore',
  237.             'help']
  238.         for name in names:
  239.             method = getattr(self.ui, name)
  240.             
  241.             wrapper = lambda options, args, name = (name,): setattr(self, 'operation', name)
  242.             setattr(self.ui, name, wrapper)
  243.         
  244.  
  245.     
  246.     def testRunCallsShowCruft(self):
  247.         self.setup_run()
  248.         self.ui.run(self.options, [
  249.             'find'])
  250.         self.assertEqual(self.operation, 'show_cruft')
  251.  
  252.     
  253.     def testRunCallsCleanup(self):
  254.         self.setup_run()
  255.         self.ui.run(self.options, [
  256.             'cleanup'])
  257.         self.assertEqual(self.operation, 'cleanup')
  258.  
  259.     
  260.     def testRunCallsIgnore(self):
  261.         self.setup_run()
  262.         self.ui.run(self.options, [
  263.             'ignore'])
  264.         self.assertEqual(self.operation, 'ignore')
  265.  
  266.     
  267.     def testRunCallsUnignore(self):
  268.         self.setup_run()
  269.         self.ui.run(self.options, [
  270.             'unignore'])
  271.         self.assertEqual(self.operation, 'unignore')
  272.  
  273.     
  274.     def testRunCallsHelp(self):
  275.         self.setup_run()
  276.         self.ui.run(self.options, [
  277.             'help'])
  278.         self.assertEqual(self.operation, 'help')
  279.  
  280.     
  281.     def testRunCallsHelpWhenThereAreNoArguments(self):
  282.         self.setup_run()
  283.         self.ui.run(self.options, [])
  284.         self.assertEqual(self.operation, 'help')
  285.  
  286.     
  287.     def testRaisesExceptionForUnknownCommand(self):
  288.         self.assertRaises(computerjanitor.Exception, self.ui.run, self.options, [
  289.             'yikes'])
  290.  
  291.     
  292.     def testRunLoadsStateFromRightFile(self):
  293.         self.setup_run()
  294.         self.ui.run(self.options, [
  295.             'ignore'])
  296.         self.assertEqual(self.app.state.load_filename, self.options.state_file)
  297.  
  298.  
  299.